package com.supermarket.servlet;

import com.supermarket.entity.Bill;
import com.supermarket.entity.Provider;
import com.supermarket.service.Adminservice;
import com.supermarket.service.ProviderService;
import com.supermarket.service.lmpl.AdminserviceImpl;
import com.supermarket.service.lmpl.ProviderServiceImpl;
import com.supermarket.util.currentPage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BillServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter ( "action" );

        if(action.equals ( "fenye" )){
            this.getPagination ( req,resp );
        }else if (action.equals ( "update" )){
            this.billupdate(req,resp);
        }else if (action.equals ( "add" )){
            this.add(req,resp);

        }
    }

    private void show(HttpServletRequest req, HttpServletResponse resp) {

        Adminservice adminservice = new AdminserviceImpl ();
        List<Provider> provider = adminservice.getProvider ();

        req.setAttribute ( "providers", provider );
        try {
            req.getRequestDispatcher ( "jsp/admin_bill_add.jsp" ).forward ( req, resp );
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    //修改的方法
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter ( "action" );
        if (action.equals ( "fenye" )){
            this.getPagination(req,resp);

        }else if (action.equals ( "search" )){
            this.getsearch(req,resp );

        }else  if (action.equals ( "del" )){
            this.delete(req,resp);
        }else if (action.equals ( "show" )){
            this.show(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter ( "id" );
        Adminservice adminservice=new AdminserviceImpl ();
        int remove = adminservice.remove ( Integer.valueOf ( id ) );
        if (remove==1){
            try {
                resp.sendRedirect ( "/BillServlet?action=fenye" );
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }else {
            try {
                resp.sendRedirect ( "/BillServlet?action=search" );
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }


    }

    private void billupdate(HttpServletRequest req, HttpServletResponse resp) {
        String providerId = req.getParameter ( "providerId" );//要修改的供应商编号
        String isPay = req.getParameter ( "isPay" );//提交的是否修付款的之0：1
        String productDesc = req.getParameter ( "productDesc" );//修改的商品描述
        String billMoney = req.getParameter ( "billMoney" );//一共的交易金额
        String counts = req.getParameter ( "counts" );//交易的数量
        String unit = req.getParameter ( "unit" );//交易的单位
        Integer id = Integer.valueOf ( req.getParameter ( "id" ) );//单号唯一标识，
        String productName = req.getParameter ( "productName" );//商品名称
        Bill bill=new Bill();
        bill.setProvider_id ( Integer.parseInt ( providerId ) );
        bill.setUnit (  unit);
        bill.setProduct_name (  productName);
        bill.setId ( id );
        bill.setBill_money ( Double.parseDouble ( billMoney ) );
        bill.setCounts ( Integer.parseInt ( counts ) );
        bill.setIs_pay ( Integer.parseInt ( isPay ) );
        bill.setProduct_desc ( productDesc );
        Adminservice adminservice=new AdminserviceImpl ();
        int res=adminservice.seriveupdate(bill);
        if (res==1){
            try {
                resp.sendRedirect ( "/BillServlet?action=fenye" );
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }else {
            try {
                req.getRequestDispatcher ( "jsp/bill_detail.jsp" ).forward ( req,resp );
            } catch (ServletException e) {
                e.printStackTrace ();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }



    }


    private void getPagination(HttpServletRequest req, HttpServletResponse resp) {

        String parameter = req.getParameter ("pageIndex");
        String name = req.getParameter ( "productName" );
        String type=req.getParameter ( "payStatus" );
        int pageIndex = 1;//这里表示初始化为1；
        int pagesize = 2;//表是每页有2条数据
        Integer parseInt=null;
        //第一次这个东西是没有数据的
        if (parameter != null&&(!parameter.equals ( "" ))) {
            //这里表示有数据才可以转换
            pageIndex = Integer.valueOf ( parameter );
        }
        if (type!=null&&(!type.equals ( "" ))){
            parseInt = Integer.parseInt ( type );
        }

        Adminservice adminservice= new AdminserviceImpl ();
        int count = adminservice.count ( name,parseInt );

        currentPage page = new currentPage (  );
        page.setSetTotalCount (  count);
        page.setTotalPageCount ( (count % pagesize) == 0 ? count / pagesize : (count / pagesize) + 1 );



        page.setPageSize ( pagesize );
        page.setCurrentPage(pageIndex);
        List<Bill> bills = adminservice.dd ( pageIndex, pagesize, name,parseInt );
        req.setAttribute ( "bills",bills);
        req.setAttribute ( "page",page );
        if (name!=null&&!name.equals ( "" )){
            req.setAttribute ( "name",name );
        }

        try {
            req.getRequestDispatcher ( "/jsp/admin_bill_list.jsp" ).forward ( req,resp );
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
    private void getsearch(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter ( "id" );
        Adminservice adminservice=new AdminserviceImpl ();
        Integer value = Integer.valueOf ( id );
        Bill bills = adminservice.select ( value );
        List<Provider> providers=adminservice.getProvider();
        req.setAttribute ( "bills",bills );
        req.setAttribute ( "providers",providers );
        try {
            req.getRequestDispatcher ( "/jsp/bill_detail.jsp" ).forward ( req,resp );
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }

    }
    private void add(HttpServletRequest req, HttpServletResponse resp) {
        String productId = req.getParameter ( "productId" );//商品编号
        String name = req.getParameter ( "productName" );//商品名字
        String money = req.getParameter ( "billMoney" );//交易金额
        String unit = req.getParameter ( "unit" );//单位
        String counts = req.getParameter ( "counts" );//数量
        String productDesc = req.getParameter ( "productDesc" );//商品描述
        String isPay = req.getParameter ( "isPay" );//是否付款
        String providerId = req.getParameter ( "providerId" );//供应商
        Bill bill= new Bill ();
        bill.setId ( Integer.parseInt ( productId ) );
        bill.setUnit (unit);
        bill.setProduct_desc ( productDesc );
        bill.setIs_pay ( Integer.parseInt ( isPay ) );
        bill.setBill_money ( Double.parseDouble ( money ) );
        bill.setProvider_id ( Integer.parseInt ( providerId ) );
        bill.setCounts ( Integer.parseInt ( counts ) );
        bill.setProduct_name ( name );
        Adminservice service=new AdminserviceImpl ();
        int adddao = service.addBull ( bill );
        if (adddao==1){
            try {
                resp.sendRedirect ( "/BillServlet?action=fenye" );
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }else {
            try {
                req.getRequestDispatcher ( "//ProviderServlet?action=show" ).forward ( req,resp );
            } catch (ServletException e) {
                e.printStackTrace ();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }



    }
    //删除
}
